package com.atguigu.dwm.model.controller;

import com.atguigu.dwm.common.bean.Result;
import com.atguigu.dwm.common.component.TableHiveProcessor;
import com.atguigu.dwm.model.bean.DmTableSync;
import com.atguigu.dwm.model.service.DmTableSyncService;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 数据同步信息表 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/data-model/sync")
public class DmTableSyncController {

    @Autowired
    TableHiveProcessor tableHiveProcessor;

    @Autowired
    DmTableSyncService dmTableSyncService;

    @GetMapping("/schema/options")
    public Result getSchemaNameOptions(){
        List<String>  schemaNameList= null;
        try {
            schemaNameList = tableHiveProcessor.getDatabaseNameList();
        } catch (Exception e) {
           e.printStackTrace();
           return  Result.error("获取Hive数据库列表失败");
        }
        return  Result.ok(schemaNameList);
    }

    @GetMapping("/list")
    public Result getSyncList(@RequestParam("schemaName") String schemaName){
        List<DmTableSync> dmTableSyncList= null;
        try {
            dmTableSyncList = dmTableSyncService.getSyncList(schemaName);
        } catch (Exception e) {
            e.printStackTrace();
            return  Result.error("获取Hive数据表列表失败");
        }

        return  Result.ok(dmTableSyncList);
    }

    // 保存同步信息列表
    @PostMapping("/list")
    public Result  saveSyncList(@RequestBody  List<DmTableSync> tableSyncList){

        dmTableSyncService.saveOrUpdateBatch(tableSyncList);
        return Result.ok(tableSyncList);

    }

    @PostMapping("/meta")
    public Result syncMeta(@RequestBody  List<DmTableSync> tableSyncList)  {
        try {
            dmTableSyncService.syncMeta(tableSyncList);
        } catch (TException e) {
             e.printStackTrace();
             return  Result.error("同步hive元数据失败");
        }
        return  Result.ok(tableSyncList);

    }

    @PostMapping("/datainfo")
    public Result syncDataInfo(@RequestBody  List<DmTableSync> tableSyncList)  {
        try {
            dmTableSyncService.syncDataInfo(tableSyncList);
        } catch (Exception e) {
            e.printStackTrace();
            return  Result.error("同步hive统计信息失败");
        }
        return  Result.ok();

    }

}
